/**
 * 
 */
package hu.gbalage.owl.cms.disec.flickr.auth;

import hu.gbalage.owl.cms.disec.PMF;
import hu.gbalage.owl.cms.disec.flickr.FlickrUser;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;

import com.aetrion.flickr.auth.Auth;
import com.aetrion.flickr.auth.Permission;
import com.aetrion.flickr.people.User;
import com.aetrion.flickr.util.AuthStore;

/**
 * @author balage
 *
 */
public class JDOAuthStore implements AuthStore {
	
	private Permission getPermission(int p){
		switch(p){
		case Permission.DELETE_TYPE : return Permission.DELETE;
		case Permission.READ_TYPE : return Permission.READ;
		case Permission.WRITE_TYPE : return Permission.WRITE;
		}
		return Permission.NONE;
	}
	
	/* (non-Javadoc)
	 * @see com.aetrion.flickr.util.AuthStore#clear(java.lang.String)
	 */
	@Override
	public void clear(String id) {
		PersistenceManager pm = PMF.getManager();

		try{
        	FlickrUser fu = pm.getObjectById(FlickrUser.class,"flickr_"+id);
        	pm.deletePersistent(fu);
        }finally{
        	pm.close();
        }
	}

	/* (non-Javadoc)
	 * @see com.aetrion.flickr.util.AuthStore#clearAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void clearAll() {
		PersistenceManager pm = PMF.getManager();
		try{
			javax.jdo.Query q = pm.newQuery(FlickrUser.class);
        	List<FlickrUser> fus = (List<FlickrUser>)q.execute();
        	
        	for(int i=0;i<fus.size();i++)
        		pm.deletePersistent(fus.get(i));
        }finally{
        	pm.close();
        }

	}

	private Auth createAuth(FlickrUser fu){
		Auth a = new Auth();
    	User u = new User();
    	a.setPermission(getPermission(fu.permission));
    	a.setToken(fu.token);
    	a.setUser(u);
    	u.setId(fu.nsid);
    	u.setRealName(fu.realname);
    	u.setUsername(fu.username);
    	return a;
	}
	
	/* (non-Javadoc)
	 * @see com.aetrion.flickr.util.AuthStore#retrieve(java.lang.String)
	 */
	@Override
	public Auth retrieve(String id) {
		
		PersistenceManager pm = PMF.getManager();
		Auth a;
		
        try{
        	FlickrUser fu = pm.getObjectById(FlickrUser.class,"flickr_"+id);
        	a = createAuth(fu);
        }finally{
        	pm.close();
        }
		
		return a;
	}

	/* (non-Javadoc)
	 * @see com.aetrion.flickr.util.AuthStore#retrieveAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Auth[] retrieveAll() {
		PersistenceManager pm = PMF.getManager();
		
		Auth[] as;
		
		try{
			javax.jdo.Query q = pm.newQuery(FlickrUser.class);
        	List<FlickrUser> fus = (List<FlickrUser>)q.execute();
        	as = new Auth[fus.size()];
        	for(int i=0;i<as.length;i++)
        		as[i] = createAuth(fus.get(i));
        }finally{
        	pm.close();
        }
		
		return as;
	}

	/* (non-Javadoc)
	 * @see com.aetrion.flickr.util.AuthStore#store(com.aetrion.flickr.auth.Auth)
	 */
	@Override
	public void store(Auth auth) throws IOException {
		FlickrUser fu = new FlickrUser();
        fu.id = "flickr_"+auth.getUser().getId();
        fu.nsid = auth.getUser().getId();
        fu.token = auth.getToken();
        fu.realname = auth.getUser().getRealName();
        fu.username = auth.getUser().getUsername();
        fu.permission = auth.getPermission().getType();
        
        PersistenceManager pm = PMF.getManager();
        
        try{
        	pm.makePersistent(fu);
        }finally{
        	pm.close();
        }
	}

}
